/**
 * 
 */
package t4j.generics;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Random;

import t4j.net.mindview.util.Generator;

/**
 * @author sz
 *
 * create time：2013-1-6
 */

class Customer{
	
	private static long counter = 1;
	
	private final long id = counter++;
	
	private Customer(){}//默认构造器设为私有

	public String toString() {	return "Customer " + id;}
	
	//每次执行它都会生成一个Generator<Customer>对象
	public static Generator<Customer> generator(){//只能通过这个方法创建对象
		
		return new Generator<Customer>() {//匿名内部类实现接口
	
			public Customer next() { return new Customer();	}//创建Customer对象
		};
	}
}

class Teller{
	
	private static long counter = 1; 
	
	private final long id = counter++;
	
	private Teller(){}
	
	public String toString() { return "Teller " + id; }
	
	//其实我们不需要多个Generator对象，这里指创建一个静态对象
	public static Generator<Teller> generator = new Generator<Teller>() {

		public Teller next() {	return new Teller(); }
			
	};
}

public class BankTeller {

	public static void serve(Teller t, Customer c){
		System.out.println(t + " serves " + c);
	}
	
	public static void main(String[] args) {
		
		Random rand = new Random(47);
		
		Queue<Customer> line = new LinkedList<Customer>();
		
		Generators.fill(line, Customer.generator(), 15);//传给fill方法回调Generator接口的next()
		
		List<Teller> tellers = new ArrayList<Teller>();
		
		Generators.fill(tellers, Teller.generator, 4);
		
		for(Customer c : line)
			serve(tellers.get(rand.nextInt(tellers.size())),c);
	}
}
